package com.woniuxy.joylifecenter.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniuxy.common.entity.ForumForum;
import com.woniuxy.joylifecenter.annotation.SysLog;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.ForumInitService;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;


import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;

@Slf4j
@Controller
@Api(value = "", description = "论坛初始化controller")
public class Center_Forum_Controller {
    @Autowired
    ForumInitService forumInitService;

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/4
     * @Param [map]
     * @Description 创建新社区
     **/
    @ApiOperation(value = "/forumInit", notes = "创建新社区")
    @SysLog(value = "创建新社区")
    @RequestMapping("/forumInit")
    @ResponseBody
    public VO forumInit(@RequestBody HashMap<String, String> map) {
        System.out.println("前端过来的数据:"
                + map.get("communityNameCheck")
                + map.get("communityAddrCheck")
                + map.get("communityTelCheck"));
        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        if (map.get("communityNameCheck") == null
                || map.get("communityAddrCheck") == null
                || map.get("communityTelCheck") == null) {
            vo.setMsg("社区信息填写不完整，请检查");
        } else {
            try {
                ForumForum forumForum = new ForumForum();
                forumForum.setDate(new Date());
                forumForum.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
                forumForum.setName(map.get("communityNameCheck"));
                forumForum.setAddress(map.get("communityAddrCheck"));
                forumForum.setPhone(map.get("communityTelCheck"));
                int count = forumInitService.forumInit(forumForum);
                vo.setMsg("成功创建社区记录");
                //此处直接从Service返回的长度获取记录条数
                vo.setCount(count);
            } catch (CenterException e) {
                e.printStackTrace();
                vo.setMsg(e.getMessage());

            }
        }

        return vo;
    }

    @ApiOperation(value = "/showForumByCondition", notes = "多条件模糊查询社区")
    @ResponseBody
    @SysLog(value = "多条件模糊查询社区")
    @RequestMapping("/showForumByCondition")
    public VO showForumByCondition(@RequestBody HashMap<String, String> pageMap) {
        System.out.println("接收到的前端参数:" + pageMap);
        ForumForum forumForum = new ForumForum();
        //JSON转Map
        String jsonstr = pageMap.get("jsonstr");
        System.out.println(jsonstr);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = new HashMap<String, String>();
        try {
            map = (HashMap<String, String>) mapper.readValue(jsonstr, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!map.get("date").equals("")) {
            try {
                forumForum.setDate(DateFormat.getDateTimeInstance().parse(map.get("date")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        forumForum.setId(map.get("id"));
        forumForum.setName(map.get("name"));
        forumForum.setAddress(map.get("addr"));
        forumForum.setPhone(map.get("phone"));
        PageHelper.startPage(Integer.parseInt(pageMap.get("page")), Integer.parseInt(pageMap.get("limit")));
        List<ForumForum> forumForums = forumInitService.selectForumByCondition(forumForum);
        PageInfo pageInfo = new PageInfo(forumForums);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        vo.setData(pageInfos);
        //此处需要从pageInfo获取总记录条数，不能直接用Service返回的集合大小
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        System.out.println("返回给前端的VO:" + vo);
        return vo;
    }

    @ApiOperation(value = "/batchDeleteForumManually", notes = "手动批量删除社区")
    @ResponseBody
    @SysLog(value = "手动批量删除社区")
    @RequestMapping("/batchDeleteForumManually")
    public VO batchDeleteLogManually(@RequestBody List<String> ids) {

        System.out.println("controller接收到的字符串数组是：" + ids);
        int count = forumInitService.batchDeleteForumManually(ids);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("批量删除了：");
        vo.setData(null);
        vo.setCount(count);
        return vo;
    }

    @ApiOperation(value = "/deleteForumManually", notes = "手动删除单个社区")
    @ResponseBody
    @SysLog(value = "手动删除单个社区")
    @RequestMapping("/deleteForumManually")
    public VO deleteLogManually(@RequestBody String id) {
        int count = forumInitService.deleteForumManually(id);
        System.out.println(id);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("本次删除了：");
        vo.setData(null);
        vo.setCount(count);
        return vo;
    }

    @ApiOperation(value = "/editForum", notes = "编辑社区记录")
    @ResponseBody
    @SysLog(value = "编辑社区记录")
    @RequestMapping("/editForum")
    public VO editForum(@RequestBody HashMap<String, String> map) {
        VO vo = new VO();
        int count = 0;
        System.out.println("前端过来的数据：" + map);
        if (map.containsKey("communityId") || (map.get("communityId") != null) || !map.get("communityId").equals("")) {
            ForumForum forumForum = new ForumForum();
            forumForum.setId(map.get("communityId"));
            forumForum.setName(map.get("communityName"));
            forumForum.setPhone(map.get("communityTel"));
            forumForum.setAddress(map.get("communityAddr"));
            forumForum.setDate(new Date());
            try {
                count = forumInitService.updateForum(forumForum);
                vo.setMsg("本次更新了：");
            } catch (CenterException e) {
                vo.setMsg(e.getMessage());
            }
        } else {
            vo.setCount(count);
        }

        vo.setCode(0);

        vo.setData(null);
        vo.setCount(count);

        return vo;
    }

    @ApiOperation(value = "/showAllForumAdminApply", notes = "查询所有待审批的社区管理员申请")
    @ResponseBody
    @SysLog(value = "查询所有待审批的社区管理员申请")
    @RequestMapping("/showAllForumAdminApply")
    public VO showAllForumAdminApply() throws JsonProcessingException {
        VO vo = new VO();
        int count = 0;
        List<Map<String, Object>> listmap = forumInitService.queryAllForumAdminApply();
        if (listmap != null) {
            count = listmap.size();
//            List<Object> list = new ArrayList<Object>();
//            list.add(map);
            vo.setData(listmap);
            vo.setCount(count);
        } else {
            vo.setData(null);
            vo.setCount(count);
        }
        vo.setCode(0);
        vo.setMsg("");

        System.out.println("后端返回的VO:" + vo);
        return vo;
    }

    @ApiOperation(value = "/rejectForumAdmin", notes = "审核拒绝")
    @SysLog(value = "审核拒绝")
    @RequestMapping("/rejectForumAdmin")
    @ResponseBody
    public VO rejectForumAdmin(@RequestBody HashMap<String, String> map) {
        VO vo = new VO();
        vo.setCode(0);
        String id = map.get("id");
        int count = forumInitService.rejectForumAdminApply(id);
        if(count==1){
            vo.setMsg("已拒绝该申请");
        }else{
            vo.setMsg("操作未成功");
        }

        vo.setCount(count);
        vo.setData(null);
        return vo;
    }

    @ApiOperation(value = "/passForumAdmin", notes = "审核通过")
    @SysLog(value = "审核通过")
    @RequestMapping("/passForumAdmin")
    @ResponseBody
    public VO passForumAdmin(@RequestBody HashMap<String, String> map) {
        VO vo = new VO();
        vo.setCode(0);
        String id = map.get("id");
        String userid = map.get("userid");
        String forumid = map.get("forumid");
        int count = forumInitService.passForumAdminApply(id, userid, forumid);
        if(count==1){
            vo.setMsg("已通过该申请");
        }else{
            vo.setMsg("操作未成功");
        }

        vo.setCount(count);
        vo.setData(null);
        return vo;
    }


}
